/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication4.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javaapplication4.controller.AbstractController;
import javaapplication4.model.observer.Observer;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;

/**
 *
 * @author mathieubakrim
 */
public class ContactForm extends JFrame implements Observer{
    private JPanel container = new JPanel();
    
    String[] tab_string = {"lastName", "firstName", "phoneNumber", "addressPro", 
        "addressPer", "homePage", "emailPro", "emailPer"};
    
    JLabel[] tab_label = new JLabel[tab_string.length];
    JTextField[] tab_textField = new JTextField[tab_string.length];
    
    private AbstractController controller;
    
    public ContactForm(AbstractController controller){
        this.setSize(450, 470);
        this.setTitle("Create a contact");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        initComposant();
        this.controller = controller;
        this.setContentPane(container);
        this.setVisible(true);
    }

    private void initComposant() {
        
        JPanel basic = new JPanel();
        basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
        add(basic);
       
        JPanel topPanel = new JPanel(new BorderLayout(0, 0));
        //topPanel.setMaximumSize(new Dimension(450, 0));
        JLabel title = new JLabel("Create a Contact");
        title.setBorder(BorderFactory.createEmptyBorder(0, 25, 0, 0));
        topPanel.add(title);
        
        JSeparator separator = new JSeparator();
        separator.setForeground(Color.gray);

        topPanel.add(separator, BorderLayout.SOUTH);

        basic.add(topPanel);
        
        
        JPanel form = new JPanel();     
        
        form.setBorder(BorderFactory.createEmptyBorder(8, 2, 5,5));
        form.setLayout(new GridLayout(8,2,5,5));

        
        for(int i = 0; i < tab_string.length; i++){
            tab_label[i] = new JLabel(tab_string[i]);
            form.add(tab_label[i]);
            tab_textField[i] = new JTextField();
            form.add(tab_textField[i]);
        }
        basic.add(form);
        
        JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT));

        JButton cancel = new JButton("Cancel");
        System.out.println("1");
        cancel.addActionListener(new CancelListener());
        
        
        
        JButton confirm = new JButton("Confirm");
        confirm.addActionListener(new CreateListener());
        
        bottom.add(cancel);
        bottom.add(confirm);
        
        basic.add(bottom);
        
        container.add(basic);
        
      
        
    } 

    @Override
    public void update(String str) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    class CreateListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
                    
                controller.createContact(tab_textField); 
                JOptionPane.showMessageDialog(null, "Person Saved");

                for(int i=0; i<tab_textField.length;i++){
                    tab_textField[i].setText("");
                    
                }
            
        }
    }
    
    class CancelListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
                setVisible(false); //you can't see me!
                dispose(); //Close the form window
            
        }
    }
    


}    
